package com.hskn.hss.module.tlemployee.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.annotation.DataScope;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.entity.TreeEntity;
import com.hskn.hss.exception.ExcelReaderDataException;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.depart.entity.SysDepart;
import com.hskn.hss.module.depart.mapper.SysDepartMapper;
import com.hskn.hss.module.depart.service.ISysDepartService;
import com.hskn.hss.module.grid.entity.Grid;
import com.hskn.hss.module.grid.mapper.GridMapper;
import com.hskn.hss.module.gridbind.entity.GridBind;
import com.hskn.hss.module.gridbind.mapper.GridBindMapper;
import com.hskn.hss.module.rptEmployeeWork.entity.RptEmployeeWork;
import com.hskn.hss.module.rptEmployeeWork.mapper.RptEmployeeWorkMapper;
import com.hskn.hss.module.tlchannel.entity.TlChannel;
import com.hskn.hss.module.tlchannel.mapper.TlChannelMapper;
import com.hskn.hss.module.tlemployee.entity.TlEmployee;
import com.hskn.hss.module.tlemployee.mapper.TlEmployeeMapper;
import com.hskn.hss.module.tlemployee.service.ITlEmployeeService;
import com.hskn.hss.module.tlemployee.to.TIEmployeeTo;
import com.hskn.hss.module.tlemployee.vo.TlEmployeePageVo;
import com.hskn.hss.module.tlequip.mapper.TlEquipMapper;
import com.hskn.hss.module.tlequipbind.mapper.TlEquipBindMapper;
import com.hskn.hss.module.tlthreshold.entity.TlThreshold;
import com.hskn.hss.module.tlthreshold.mapper.TlThresholdMapper;
import com.hskn.hss.module.tlworktype.entity.TlWorkType;
import com.hskn.hss.module.tlworktype.mapper.TlWorkTypeMapper;
import com.hskn.hss.module.user.entity.User;
import com.hskn.hss.module.user.mapper.UserMapper;
import com.hskn.hss.module.user.service.IUserService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.AttributeUtils;
import com.hskn.hss.utils.GroupingByUtils;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.TreeUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-15
 */
@Service
public class TlEmployeeServiceImpl extends ServiceImpl<TlEmployeeMapper, TlEmployee> implements ITlEmployeeService {
    @Resource
    TlEmployeeMapper tlEmployeeMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;
    @Resource
    SysDepartMapper sysDepartMapper;
    @Autowired
    IUserService userService;
    @Resource
    TlThresholdMapper tlThresholdMapper;
    @Resource
    TlEquipBindMapper tlEquipBindMapper;
    @Resource
    TlChannelMapper tlChannelMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    TlWorkTypeMapper tlWorkTypeMapper;
    @Resource
    TlEquipMapper tlEquipMapper;
    @Resource
    GridBindMapper gridBindMapper;
    @Resource
    private GridMapper gridMapper;
    @Resource
    private SysDepartMapper departMapper;
    @Resource
    ISysDepartService departService;
    @Resource
    private RptEmployeeWorkMapper rptEmployeeWorkMapper;

    @Override
    @Transactional //添加事务
    public AjaxResult insert(TlEmployee tlEmployee) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();
        if(StringUtils.isNotEmpty(tlEmployee.getEmplnum())){
            QueryWrapper<TlEmployee> tlEmployeeQueryWrapper = new QueryWrapper<>();
            tlEmployeeQueryWrapper.lambda().eq(TlEmployee::getEmplnum,tlEmployee.getEmplnum());
            List<TlEmployee> checkTl = tlEmployeeMapper.selectList(tlEmployeeQueryWrapper);
            if(CollectionUtils.isNotEmpty(checkTl) && checkTl.size()>0){
                throw new HssException("工号重复");
            }
        }


        tlEmployee.setCreateby(uid);
        tlEmployee.setCreatetime(new Date());
        tlEmployee.setDeptid(deptid);
        if (tlEmployee.getStatus() == null || tlEmployee.getStatus().isEmpty()) {
            tlEmployee.setStatus("1");
        }
        tlEmployeeMapper.insert(tlEmployee);

        Map map = new HashMap();
        map.put("thresholdtype", "person");
        List<TlThreshold> tlThresholdList = tlThresholdMapper.selectByMap(map);
        for (TlThreshold t : tlThresholdList) {
            t.setThresholdtype("item");
            t.setItemid(tlEmployee.getId());
            t.setId(StringUtils.uuid());
            tlThresholdMapper.insert(t);
        }
//        if(tlEmployee.getIscreatecount().equals("1")){ //创建账户 添加角色 、部门关联关系
//            JSONObject param = new JSONObject();
//            param.put("account",tlEmployee.getAccount());
//            param.put("password",tlEmployee.getPassword());
//            param.put("roleids",tlEmployee.getRole());
//            param.put("depid",tlEmployee.getEmpldeptid());
//            param.put("name",tlEmployee.getName());
//            param.put("platform",0);
//            param.put("phone",tlEmployee.getPhonenum());
//            param.put("isDepAdmin ",2);
////            param.put("patrolUid",3);
//            param.put("remark","创建人员档案时同步创建用户");
//            userService.addUser(uid,param);
//
//        }
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult update(TlEmployee tlEmployee) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        String ids = tlEmployee.getIds();
        String[] idArray = ids.split(",");
        tlEmployee.setUpdateby(uid);
        tlEmployee.setUpdatetime(new Date());
        if (tlEmployee.getStatus() != null && tlEmployee.getStatus().equals("0")) {
            tlEmployee.setQuittime(new Date());
        }
        tlEmployeeMapper.updateById(tlEmployee);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult updatebyids(TlEmployee tlEmployee) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        String ids = tlEmployee.getIds();
        String[] idArray = ids.split(",");
        tlEmployee.setUpdateby(uid);
        tlEmployee.setUpdatetime(new Date());
        if (tlEmployee.getStatus() != null && tlEmployee.getStatus().equals("0")) {
            tlEmployee.setQuittime(new Date());
        }
        UpdateWrapper<TlEmployee> tlEmployeeUpdateWrapper = new UpdateWrapper<>();
        tlEmployeeUpdateWrapper.lambda().in(TlEmployee::getId, idArray);

        tlEmployeeMapper.update(tlEmployee, tlEmployeeUpdateWrapper);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlEmployeeMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult deletebatchbyid(TlEmployee tlEmployee) throws HssException {
        String ids = tlEmployee.getIds();
        String[] idArray = ids.split(",");
        tlEmployeeMapper.deleteBatchIds(Arrays.asList(idArray));
        return AjaxResult.success("操作成功");
    }

    @Override
    public TlEmployee getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        QueryWrapper<TlEmployee> tlEmployeeQueryWrapper = new QueryWrapper<>();
        tlEmployeeQueryWrapper.lambda().eq(TlEmployee::getId, id);
        TlEmployee tlEmployee = tlEmployeeMapper.selectOne(tlEmployeeQueryWrapper);
        String equipid = tlEmployee.getEquipid();
        if (!StringUtils.isEmpty(equipid)) {
            QueryWrapper<TlChannel> tlChannelQueryWrapper = new QueryWrapper<>();
            tlChannelQueryWrapper.lambda().eq(TlChannel::getEquipid, equipid);
            tlChannelQueryWrapper.lambda().orderByAsc(TlChannel::getChannelname);
            List details = tlChannelMapper.selectList(tlChannelQueryWrapper);
            tlEmployee.setChannels(details);
        }
        String bindaccount = tlEmployee.getBindaccount();
        if (!StringUtils.isEmpty(bindaccount)) {
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.lambda().eq(User::getUid, bindaccount);
            User user = userMapper.selectOne(userQueryWrapper);
            tlEmployee.setUserName(user.getName());
        }
        SysDepart sysDepart = sysDepartMapper.selectById(tlEmployee.getEmpldeptid());
        if (null != sysDepart) {
            tlEmployee.setEmpldeptname(sysDepart.getDepartName());
        }
        TlWorkType tlWorkType = tlWorkTypeMapper.selectById(tlEmployee.getWorktype());
        if (null != tlWorkType) {
            tlEmployee.setWorktypename(tlWorkType.getWorktype());
        }
        return tlEmployee;
    }

    @Override
    public List getSelectList() {
        List resultList = tlEmployeeMapper.getSelectList();
        return resultList;
    }


    @Override
    @DataScope(deptAlias = "empldeptid", userAlias = "createby, updateby, bindaccount")
    public TlEmployeePageVo getList(BaseEntity entity, TIEmployeeTo tIEmployeeTo) throws HssException {

        QueryWrapper<GridBind> gridBindQueryWrapper = new QueryWrapper<>();
        List<GridBind> gridBindList = gridBindMapper.selectList(gridBindQueryWrapper);
        Map<String, List<GridBind>> gridBindCollect = gridBindList.stream().collect(Collectors.groupingBy(GridBind::getItemId));

        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(Collectors.toList(), grids -> grids.get(0))));

        List<SysDepart> sysDepartList = sysDepartMapper.selectList(new QueryWrapper<>());
        Map<String, SysDepart> sysDepartMap = sysDepartList.stream().collect(Collectors.groupingBy(SysDepart::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));

        List<TlWorkType> tlWorkTypeList = tlWorkTypeMapper.selectList(new QueryWrapper<>());
        Map<String, TlWorkType> tlWorkTypeMap = tlWorkTypeList.stream().collect(Collectors.groupingBy(TlWorkType::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));


        QueryWrapper<TlEmployee> employeeQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(tIEmployeeTo.getWorkType())) {
            employeeQueryWrapper.lambda().eq(TlEmployee::getWorktype, tIEmployeeTo.getWorkType());
        }
        if(StringUtils.isNotEmpty(tIEmployeeTo.getStatus())){
            employeeQueryWrapper.lambda().eq(TlEmployee::getStatus, tIEmployeeTo.getStatus());
        }
        if (StringUtils.isNotEmpty(tIEmployeeTo.getDeptId())) {
            employeeQueryWrapper.lambda().eq(TlEmployee::getDeptid, tIEmployeeTo.getDeptId());
        }
        if (StringUtils.isNotEmpty(tIEmployeeTo.getEmpldeptid())) {
            List<String> depids = departService.getChildIdsByIId(tIEmployeeTo.getEmpldeptid());
            employeeQueryWrapper.lambda().in(TlEmployee::getEmpldeptid, depids);
        }
        if (StringUtils.isNotEmpty(tIEmployeeTo.getEmployeeNumber())) {
            employeeQueryWrapper.lambda().eq(TlEmployee::getEmplnum, tIEmployeeTo.getEmployeeNumber());
        }
        if (StringUtils.isNotEmpty(tIEmployeeTo.getEmployeeName())) {
            employeeQueryWrapper.lambda().in(TlEmployee::getName, tIEmployeeTo.getEmployeeName().split(","));
        }
        if (StringUtils.isNotEmpty(tIEmployeeTo.getPhoneNumber())) {
            employeeQueryWrapper.lambda().eq(TlEmployee::getPhonenum, tIEmployeeTo.getPhoneNumber());
        }
        if (entity.getParams().containsKey("dataScope") && StringUtils.isNotEmpty(entity.getParams().get("dataScope"))) {
            employeeQueryWrapper.lambda().apply(entity.getParams().get("dataScope").toString());
        }
        List<TlEmployee> tlEmployeeList = tlEmployeeMapper.selectList(employeeQueryWrapper);
        List<String> gridIdList = new ArrayList<>();
        for (TlEmployee tlEmployee : tlEmployeeList) {
            SysDepart sysDepart = sysDepartMap.get(tlEmployee.getEmpldeptid());
            if (null != sysDepart) {
                tlEmployee.setEmpldeptname(sysDepart.getDepartName());
            }
            TlWorkType tlWorkType = tlWorkTypeMap.get(tlEmployee.getWorktype());
            if (null != tlWorkType) {
                tlEmployee.setWorktypename(tlWorkType.getWorktype());
            }
            Map param = new HashMap();
            param.put("itemid", tlEmployee.getId());
            tlEmployee.setBinds(tlEquipBindMapper.selectByMap(param));
            List<GridBind> gridBindList1 = gridBindCollect.get(tlEmployee.getId());
            if (null != gridBindList1) {
                for (GridBind gridBind : gridBindList1) {
                    Grid grid = gridMap.get(gridBind.getGridId());
                    if (null != grid) {
                        gridBind.setGridName(grid.getName());
                        tlEmployee.getGridBindList().add(gridBind);
                        gridIdList.add(grid.getGridId());
                    }
                }
            }
        }
        if (StringUtils.isNotEmpty(tIEmployeeTo.getBindingStatus()) && tIEmployeeTo.getBindingStatus() == 2) {
            List<TlEmployee> tlEmployeeList1 = tlEmployeeList.stream().filter(tlEmployee1 -> (!tlEmployee1.getGridBindList().isEmpty()) && (gridIdList.contains(tIEmployeeTo.getGridId()))).collect(toList());
            return page(tIEmployeeTo, tlEmployeeList1);
        } else if (StringUtils.isNotEmpty(tIEmployeeTo.getBindingStatus()) && tIEmployeeTo.getBindingStatus() == 1) {
            List<TlEmployee> tlEmployeeList1 = tlEmployeeList.stream().filter(tlEmployee1 -> (tlEmployee1.getGridBindList().isEmpty())).collect(toList());
            return page(tIEmployeeTo, tlEmployeeList1);
        } else {
            return page(tIEmployeeTo, tlEmployeeList);
        }
    }

    private TlEmployeePageVo page(TIEmployeeTo tIEmployeeTo, List list) {
        // 分页
        Long page = StringUtils.isEmpty(tIEmployeeTo.getPage()) ? 1 : tIEmployeeTo.getPage();
        Long size = StringUtils.isEmpty(tIEmployeeTo.getSize()) ? 10 : tIEmployeeTo.getSize();
        TlEmployeePageVo tlEmployeePageVo = new TlEmployeePageVo();

        tlEmployeePageVo.setTotal((long) list.size());
        tlEmployeePageVo.setCurrent(page);
        tlEmployeePageVo.setSize(size);
        BigDecimal pageSizeB = new BigDecimal(size);
        BigDecimal sizeB = new BigDecimal(list.size());
        tlEmployeePageVo.setPages(Long.valueOf(sizeB.divide(pageSizeB, BigDecimal.ROUND_CEILING).toString()));

        tlEmployeePageVo.setList(new ArrayList<>());
        for (int i = 0; i < list.size(); i++) {
            if (i >= size * (page - 1) && i < size * page) {
                tlEmployeePageVo.getList().add(list.get(i));
            }
        }
        return tlEmployeePageVo;
    }

    @Override
    public List<TreeEntity> getEmplTree(JSONObject params) {

        List<TreeEntity> treeList = sysDepartMapper.getTreeList();
        List<TreeEntity> emplTree = tlEmployeeMapper.getEmplTree();

        treeList.addAll(emplTree);
        List<TreeEntity> trees = TreeUtil.RecursiveAddress(treeList);
        return trees;
    }

    @Override
    public List<TreeEntity> getEmplTree2(JSONObject params) {
        String depid = params == null ? "" : params.getString("depid");

        List<String> depids = departService.getChildIdsByIId(depid);
        List<TreeEntity>treeEntityList = new ArrayList<>();
        List<TreeEntity> emplTree = tlEmployeeMapper.getEmplTree();
        for (TreeEntity treeEntity : emplTree) {
            for (String s : depids) {
                if(treeEntity.getParentid().equals(s)){
                    treeEntityList.add(treeEntity);
                }
            }
        }
        List<TreeEntity> treeList = sysDepartMapper.getTreeList();
        for (TreeEntity treeEntity : treeList) {
            for (String s : depids) {
                if(treeEntity.getId().equals(s)){
                    treeEntityList.add(treeEntity);
                }
            }
        }
        treeEntityList=treeEntityList.stream().distinct().collect(toList());
        for (TreeEntity treeEntity : treeEntityList) {
            if(treeEntity.getId().equals(depid)){
                treeEntity.setParentid("");
            }
        }

//        treeList.addAll(emplTree);
        List<TreeEntity> trees = TreeUtil.RecursiveAddress(treeEntityList);
        return trees;
    }

    @Override
    public List<TreeEntity> getuserList() {
        List<TreeEntity> treeList = sysDepartMapper.getTreeList();
        List<TreeEntity> userTree = tlEmployeeMapper.getuserList();
        treeList.addAll(userTree);
        List<TreeEntity> trees = TreeUtil.RecursiveAddress(treeList);
        return trees;
    }

    @Override
    public int getCurrentNumber(JSONObject params) throws HssException {
        QueryWrapper<TlEmployee> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TlEmployee::getStatus, 1);
        queryWrapper.lambda().eq(TlEmployee::getEmpldeptid, params.getString("empldeptid"));
        int json = this.count(queryWrapper);
        return json;
    }

    @Override
    public int getDepartureNumber(JSONObject params) throws HssException {
        QueryWrapper<TlEmployee> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TlEmployee::getStatus, 0);
        queryWrapper.lambda().eq(TlEmployee::getEmpldeptid, params.getString("empldeptid"));
        int json = this.count(queryWrapper);
        return json;
    }

    @Override
    public JSONObject getJobCategories(JSONObject params) throws HssException {
        QueryWrapper<TlEmployee> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TlEmployee::getStatus, 1);
        queryWrapper.lambda().eq(TlEmployee::getEmpldeptid, params.getString("empldeptid"));
        queryWrapper.select("persontype as name, count(1) as value");
        queryWrapper.lambda().groupBy(TlEmployee::getPersontype);
        List<Map<String, Object>> jobCategories = this.listMaps(queryWrapper);
        JSONObject json = new JSONObject();
        json.put("data", jobCategories);
        return json;
    }

    @Override
    public JSONObject getWorkerType(JSONObject params) throws HssException {
//        QueryWrapper<TlEmployee> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(TlEmployee::getStatus, 1);
//        queryWrapper.lambda().eq(TlEmployee::getEmpldeptid, params.getString("empldeptid"));
//        queryWrapper.select("worktype as name, count(1) as value");
//        queryWrapper.lambda().groupBy(TlEmployee::getWorktype);
//        List<Map<String, Object>> jobCategories=this.listMaps(queryWrapper);
        Map<String, String> map = new HashMap();
        map.put("empldeptid", params.getString("empldeptid"));
        List<Map<String, Object>> jobCategories = tlEmployeeMapper.getWorkerType(map);
        JSONObject json = new JSONObject();
        json.put("data", jobCategories);
        return json;
    }

    @Override
    public JSONObject getAge(JSONObject params) throws HssException {
        Map<String, String> map = new HashMap();
        map.put("empldeptid", params.getString("empldeptid"));
        List<Map<String, Object>> jobCategories = tlEmployeeMapper.getAge(map);
        JSONObject json = new JSONObject();
        json.put("data", jobCategories);
        return json;
    }

    @Override
    public JSONObject getGender(JSONObject params) throws HssException {
        QueryWrapper<TlEmployee> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TlEmployee::getStatus, 1);
        queryWrapper.lambda().eq(TlEmployee::getEmpldeptid, params.getString("empldeptid"));
        queryWrapper.select("sex as name, count(1) as value");
        queryWrapper.lambda().groupBy(TlEmployee::getSex);
        List<Map<String, Object>> jobCategories = this.listMaps(queryWrapper);
        JSONObject json = new JSONObject();
        json.put("data", jobCategories);
        return json;
    }

    @Override
    public JSONObject getEducation(JSONObject params) throws HssException {
        QueryWrapper<TlEmployee> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TlEmployee::getStatus, 1);
        queryWrapper.lambda().eq(TlEmployee::getEmpldeptid, params.getString("empldeptid"));
        queryWrapper.select("educationbackground as name, count(1) as value");
        queryWrapper.lambda().groupBy(TlEmployee::getEducationbackground);
        List<Map<String, Object>> jobCategories = this.listMaps(queryWrapper);
        JSONObject json = new JSONObject();
        json.put("data", jobCategories);
        return json;
    }

    @Override
    public JSONObject getNational(JSONObject params) throws HssException {
        QueryWrapper<TlEmployee> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TlEmployee::getStatus, 1);
        queryWrapper.lambda().eq(TlEmployee::getEmpldeptid, params.getString("empldeptid"));
        queryWrapper.select("nation as name, count(1) as value");
        queryWrapper.lambda().groupBy(TlEmployee::getNation);
        List<Map<String, Object>> jobCategories = this.listMaps(queryWrapper);
        JSONObject json = new JSONObject();
        json.put("data", jobCategories);
        return json;
    }

    @Override
    public JSONObject getPolitical(JSONObject params) throws HssException {
        QueryWrapper<TlEmployee> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TlEmployee::getStatus, 1);
        queryWrapper.lambda().eq(TlEmployee::getEmpldeptid, params.getString("empldeptid"));
        queryWrapper.select("politicsstatus as name, count(1) as value");
        queryWrapper.lambda().groupBy(TlEmployee::getPoliticsstatus);
        List<Map<String, Object>> jobCategories = this.listMaps(queryWrapper);
        JSONObject json = new JSONObject();
        json.put("data", jobCategories);
        return json;
    }

    @Override
    public Map<String, TlEmployee> getEmpMap(QueryWrapper<TlEmployee> empQueryWrapper) {
        List<TlEmployee> employeeList = tlEmployeeMapper.selectList(empQueryWrapper);
        Map<String, TlEmployee> empMap = new HashMap<>();
        if (!employeeList.isEmpty()) {
            empMap = GroupingByUtils.getGroupingForMapEntity(employeeList, TlEmployee::getId, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        }
        return empMap;
    }

    @Override
    public void excelReader(List<TlEmployee> tlEmployeeList) throws ExcelReaderDataException {

        String uid = LoginUtils.getUser().getUid();
        QueryWrapper<TlWorkType> tlWorkTypeQueryWrapper = new QueryWrapper<>();

        List<TlWorkType> tlWorkTypeList = tlWorkTypeMapper.selectList(tlWorkTypeQueryWrapper);
        Map<String, TlWorkType> tlWorkTypeMap = new HashMap<>();

        if (!tlWorkTypeList.isEmpty()) {
            Predicate<TlWorkType> workTypeNotNull = work -> StringUtils.isNotEmpty(work.getWorktype());
            tlWorkTypeList = tlWorkTypeList.stream().filter(workTypeNotNull).collect(Collectors.toList());
            tlWorkTypeMap = GroupingByUtils.getGroupingForMapEntity(tlWorkTypeList, TlWorkType::getWorktype, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        }
        List<SysDepart> departList = departMapper.selectList(new QueryWrapper<>());
        Map<String, SysDepart> departMap = new HashMap<>();
        if (!departList.isEmpty()) {
            Predicate<SysDepart> departNameNotNull = depart -> StringUtils.isNotEmpty(depart.getDepartName());
            departList = departList.stream().filter(departNameNotNull).collect(Collectors.toList());
            departMap = GroupingByUtils.getGroupingForMapEntity(departList, SysDepart::getDepartName, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        }
        int num = 1;
        String str = "";

        //EXCEL查重
        List<TlEmployee> tlEmployees = tlEmployeeMapper.selectList(new QueryWrapper<>());

        List<String> code = tlEmployeeList.stream().map(TlEmployee::getEmplnum).collect(Collectors.toList());
        if (AttributeUtils.RepeatedCheck(code) == false) {
            for (Object a : AttributeUtils.RepeatedList(code)) {
                str += "'第" + a + "条' 的 “工号:" + code.get((int) a - 1) + "”  重复;##";
            }
        }


        for (TlEmployee e : tlEmployeeList) {
            e.setCreatetime(new Date());
            e.setCreateby(uid);
            e.setStatus("1");
            if (departMap.containsKey(e.getEmpldeptname())) {
                e.setDeptid(departMap.get(e.getEmpldeptname()).getId());
                e.setEmpldeptid(departMap.get(e.getEmpldeptname()).getId());
            } else {
                str += "第" + num + "条的" + "“‘" + "所属机构’:‘" + e.getDeptName() + "’”" + "不存在;##";
            }
            if (tlWorkTypeMap.containsKey(e.getWorktypename())) {
                e.setWorktype(tlWorkTypeMap.get(e.getWorktypename()).getId());
            } else {
                str += "第" + num + "条的“‘工种’:‘" + e.getWorktypename() + "’”" + "不存在;##";
            }
            if (null == e.getPersontype() && e.getPersontype() == "") {
                str += "第" + num + "条的“‘作业类型’:‘" + e.getPersontype() + "’”" + "不存在;##";
            }
            if (null == e.getAttendtype() && e.getAttendtype() == "") {
                str += "第" + num + "条的“‘考勤方式’:‘" + e.getAttendtype() + "’”" + "不存在;##";
            }
            //参数判空
            if (StringUtils.isEmpty(e.getWorktypename())) {
                str += "'第" + num + "条' 的 “工种” 不能为空;##";
            }
            if (StringUtils.isEmpty(e.getName())) {
                str += "'第" + num + "条' 的 “姓名” 不能为空;##";
            }
            if (StringUtils.isEmpty(e.getEmpldeptname())) {
                str += "'第" + num + "条' 的 “所属机构” 不能为空;##";
            }
            if (StringUtils.isEmpty(e.getAttendance())) {
                str += "'第" + num + "条' 的 “考勤方式” 不能为空;##";
            }
            if (StringUtils.isEmpty(e.getPhonenum())) {
                str += "'第" + num + "条' 的 “联系电话” 不能为空;##";
            }
            //参数判空结束

            //数据库查询判断=======================================
            int ck = 0;
            for (TlEmployee al : tlEmployees) {
                if (StringUtils.isNotEmpty(al.getEmplnum()) && e.getEmplnum().equals(al.getEmplnum()) && ck == 0) {
                    str += "'第" + num + "条' 档案中已存在 “工号:" + e.getEmplnum() + "”;";
                    ck = 1;
                }
            }
            if (ck > 0) {
                str += "##";
            }
            //数据库查询判断结束=======================================

            num++;
        }
        if (StringUtils.isNotEmpty(str)) {
            throw new ExcelReaderDataException("您导入的数据:##" + str);
        }
    }

    @Override
    public List<Grid> getGridList(TIEmployeeTo tiEmployeeTo) {

        Set<String> gridIdList = new HashSet<>();
        QueryWrapper<RptEmployeeWork> rptEmployeeWorkQueryWrapper = new QueryWrapper<>();
        rptEmployeeWorkQueryWrapper.lambda().eq(RptEmployeeWork::getWorkDate, StringUtils.dateToStr(new Date(), "yyyy-MM-dd"));
        rptEmployeeWorkQueryWrapper.lambda().eq(RptEmployeeWork::getEmployeeId, tiEmployeeTo.getEmployeeId());
        List<RptEmployeeWork> rptEmployeeWorkList = rptEmployeeWorkMapper.selectList(rptEmployeeWorkQueryWrapper);
        for (RptEmployeeWork rptEmployeeWork : rptEmployeeWorkList) {
            gridIdList.add(rptEmployeeWork.getGridId());
        }
        QueryWrapper<Grid> gridQueryWrapper = new QueryWrapper<>();
        gridQueryWrapper.lambda().eq(Grid::getGridType, 0);
        gridQueryWrapper.lambda().in(Grid::getGridId, gridIdList);
        List<Grid> gridList = gridMapper.selectList(gridQueryWrapper);
        return gridList;
    }


}
